package bev.query;

import bev.article.ArticleType;
import bev.article.BaseArticle;
import bev.article.EventArticle;
import bev.article.ObjectArticle;
import bev.article.PersonArticle;
import bev.article.PlaceArticle;

//These items are displayed in the search panel JList. Each ArticleListItem represents an
// article that exists in the database. They only contain the basic information needed to
// display them in the search panel though (name, ID, and article type). When the full article
// is needed for displaying, the ArticleListItem's loadArticle method will retrieve it.
public class ArticleListItem implements Comparable<ArticleListItem> {
    ArticleType articleType;
    int id;
    String name;
    
    // Requires the article type (person, place, etc.), article database ID, and article name to create.
    public ArticleListItem( ArticleType articleType, int id, String name ) {
        this.articleType = articleType;
        this.id = id;
        this.name = name;
    }
    
    public ArticleType getArticleType() {
        return articleType;
    }
    
    public int getID() {
        return id;
    }
    
    public String getName() {
        return name;
    }
    
    // Creates and loads an article of the proper type
    // for the article this ArticleListItem represents.
    public BaseArticle loadArticle() {
        BaseArticle article = null;
        
        switch ( articleType ) {
            case PERSON:
                article = (BaseArticle) new PersonArticle();
                break;
            case PLACE:
                article = (BaseArticle) new PlaceArticle();
                break;
            case EVENT:
                article = (BaseArticle) new EventArticle();
                break;
            case OBJECT:
                article = (BaseArticle) new ObjectArticle();
                break;
            default:
                System.err.printf( "Article type not supported: %s", articleType.toString() );
                return null;
        }
        
        article.loadArticle( id );
        
        return article;
    }

    @Override
    public int compareTo(ArticleListItem other) {        
        return this.name.compareTo( other.name );
    }
    
    // Overridden to return 'name' so that articles' names are displayed
    // in the article search list. Whatever this returns will be the
    // string used to display this article in the article search list.
    @Override
    public String toString() {
        return name;
    }
}
